package com.wiloon.java8x.io.datastream;

/**
 * Created by roy.wang on 2017/4/20;
 */

import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;

public class DataStreamDemo {
    public static void main(String[] args) throws URISyntaxException {
        Member[] members = {
                new Member("Justin", 90),
                new Member("momor", 95),
                new Member("Bush", 88)
        };
        try {
            URL to = ClassLoader.getSystemClassLoader().getResource("bar.txt");
            File file = new File(to.toURI());
            DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(file));

            for (Member member : members) {
                // 写入UTF字符串
                dataOutputStream.writeUTF(member.getName());
                // 写入int数据
                dataOutputStream.writeInt(member.getAge());
            }
            // 读出所有数据至目的地
            dataOutputStream.flush();
            // 关闭流
            dataOutputStream.close();

            DataInputStream dataInputStream = new DataInputStream(new FileInputStream(file));
            // 读出数据并还原为对象
            for (int i = 0; i < members.length; i++) {
                // 读出UTF字符串
                String name = dataInputStream.readUTF();
                // 读出int数据
                int score = dataInputStream.readInt();
                members[i] = new Member(name, score);
            }
            // 关闭流
            dataInputStream.close();
            // 显示还原后的数据
            for (Member member : members) {
                System.out.printf("%s\t%d%n", member.getName(), member.getAge());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}